<?php

namespace App\Http\Controllers\Api\Purchase;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Purchase\Vendor as Model;

class Vendor extends Controller
{

    /** 新增或编辑供应商
     * @param $name
     * @param $tel
     * @param $manager
     * @param $manager_email
     * @param $manager_phone
     * @param $country
     * @param $province
     * @param $city
     * @param $addr
     * @param Request $req
     * @return array
     */
    public function save(Request $req)
    {
        if ($id = $req->post('id')) {
            $obj = Model::find($id);
            if (!$obj) {
                return self::resFailOfNoData(Model::NAME);
            }
        } else {
            $obj = new Model();
        }

        $obj->fill($req->post());

        if (!$obj->name) {
            $this->resFail('供应商名称不能为空！');
        }

        return $obj->save() ? $this->resSuccess('操作成功', $obj)
            : $this->resFail('保存失败');

    }

    /**
     * 供应商列表
     * @param Request $req
     * @return array
     * @url purchase/vendor
     */
    public function ls(Request $req)
    {
        $num_per_page = $req->num_per_page ?: 20;
        $obj = Model::query();

        if ($req->simple) { #无分页
            $res = $obj->select('id', 'name')->get();
            return self::resSuccess('', $res);
        }
        $obj->select('name', 'id', 'status');

        $res = $obj->paginate($num_per_page);
        return $this->resSuccess(
            '列表成功。',
            $res
        );
    }




    /**
     * 供应商明细
     * @param $id
     * @param Request $req
     * @return array
     */
    public function detail(Request $req)
    {
        $res = Model::find($req->id);

        return $this->resSuccess(
            '成功。',
            $res
        );
    }



    ### 下面是 Resource 模式  ###

    /**
     * 供应商明细   GET
     * @param $id
     * @param Request $req
     * @return array
     */
    public function show($id)
    {
        $res = Model::find($id);

        return $this->resSuccess(
            '成功。',
            $res
        );
    }


    /**
     * 供应商列表   GET
     * @param Request $req
     * @return array
     * @url purchase/vendor
     */
    public function index(Request $req)
    {
        $num_per_page = $req->per_page ?: 20;
        $obj = Model::query();

        if ($req->simple) { #无分页
            $res = $obj->select('id', 'name')->get();
            return self::resSuccess('', $res);
        }
        $obj->select('name', 'id', 'status');

        $res = $obj->paginate($num_per_page);
        return $this->resSuccess(
            '列表成功。',
            $res
        );
    }


    /** 新增供应商  POST
     * @param $name
     * @param $tel
     * @param $manager
     * @param $manager_email
     * @param $manager_phone
     * @param $country
     * @param $province
     * @param $city
     * @param $addr
     * @param Request $req
     * @return array
     */
    public function store(Request $req)
    {

        $obj = new Model();

        $obj->fill($req->post());

        if (!$obj->name) {
            $this->resFail('供应商名称不能为空！');
        }

        return $obj->save() ? $this->resSuccess('操作成功', $obj)
            : $this->resFail('保存失败');

    }
    /** 保存供应商  PUT
     * @param $name
     * @param $tel
     * @param $manager
     * @param $manager_email
     * @param $manager_phone
     * @param $country
     * @param $province
     * @param $city
     * @param $addr
     * @param Request $req
     * @return array
     */
    public function update(Request $req, $id)
    {

        $obj = Model::find($id);
        if (!$obj) {
            return self::resFailOfNoData(Model::NAME);
        }

        $obj->fill($req->input());

        if (!$obj->name) {
            $this->resFail('供应商名称不能为空！');
        }

        return $obj->save() ? $this->resSuccess('操作成功', $obj)
            : $this->resFail('保存失败');

    }


    /**
     * 删除供应商  DELETE
     *
     */
    public function destroy($id)
    {
        Model::where('id', $id)->delete();

        return $this->resSuccess('操作成功');
    }

}
